package juniors.servlet;


import java.io.IOException;
import java.util.Collection;

import juniors.modelo.CentroEstudio;
import juniors.modelo.Idea;
import juniors.negocio.GestionCentroEstudio;
import juniors.negocio.GestionIdeas;
import juniors.dao.IdeaDAO;
import juniors.excepcion.DAOExcepcion;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BuscarIdea
 */
@WebServlet("/BuscarIdea")
public class IdeaBuscarServlet extends javax.servlet.http.HttpServlet implements
	javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdeaBuscarServlet() {
        super();
        System.out.println("servlet");
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("get");
		String titulo = request.getParameter("nombre");
		System.out.println(titulo);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
System.out.println("post");
String titulo = request.getParameter("nombre");
System.out.println(titulo);
	Idea idea = new Idea();
	IdeaDAO dao = new IdeaDAO();
	//dao.buscarIdea(titulo, "ss");
	GestionIdeas negocio = new GestionIdeas();
	try {
		Collection<Idea> lista = negocio.buscarIdeaXtitulo(titulo, "");
		System.out.println("Iniciar busqueda");
			for(Idea x : lista)	{
				System.out.println(x.getTitulo()+" "+x.getDescripcion());
				
			}
			System.out.println("fin busqueda");	
		// Guardar en el ambiente de request
		request.setAttribute("ideaListado", lista);
		RequestDispatcher rd = request.getRequestDispatcher("idea_buscar.jsp");
		rd.forward(request, response);

	} catch (DAOExcepcion e) {
		System.out.println(e.getMessage());
		RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
		rd.forward(request, response);
	}
	}

}
